package cn.tpshion.vm.common.utils;

import cn.tpshion.vm.common.constant.ErrorCode;
import cn.tpshion.vm.common.exception.BizException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class AssertUtil {

    private AssertUtil() {

    }

    public static void isEmpty(Object obj, String msg){
        isEmpty(obj, ErrorCode.FAIL, msg);
    }

    public static void isEmpty(Object obj, ErrorCode errorCode){
        isEmpty(obj, errorCode, errorCode.getMsg());
    }

    public static void isEmpty(Object obj, ErrorCode errorCode, String msg){
        if(ObjectUtils.isEmpty(obj)){
            throw new BizException(errorCode.getCode(), msg);
        }
    }

    public static void isNotEmpty(Object obj, String msg){
        if(!ObjectUtils.isEmpty(obj)){
            throw new BizException(ErrorCode.FAIL.getCode(), msg);
        }
    }

    public static void isBlank(String value, String msg){
        isBlank(value, ErrorCode.FAIL, msg);
    }

    public static void isNotBlank(String value, String msg){
        if(StringUtils.isNotBlank(value)){
            throw new BizException(ErrorCode.FAIL.getCode(), msg);
        }
    }

    public static void isBlank(String value, ErrorCode errorCode){
        isBlank(value, errorCode, errorCode.getMsg());
    }

    public static void isBlank(String value, ErrorCode errorCode, String msg){
        if(StringUtils.isBlank(value)){
            throw new BizException(errorCode.getCode(), msg);
        }
    }

    public static void isTrue(boolean flag, String msg){
        isTrue(flag, ErrorCode.FAIL, msg);
    }

    public static void isTrue(boolean flag, ErrorCode errorCode){
        isTrue(flag, errorCode, errorCode.getMsg());
    }

    public static void isTrue(boolean flag, ErrorCode errorCode, String msg){
        if(flag){
            throw new BizException(errorCode.getCode(), msg);
        }
    }

    public static void isFalse(boolean flag, String msg){
        isFalse(flag, ErrorCode.FAIL, msg);
    }

    public static void isFalse(boolean flag, ErrorCode errorCode){
        isFalse(flag, errorCode, errorCode.getMsg());
    }

    public static void isFalse(boolean flag, ErrorCode errorCode, String msg){
        if(flag){
            return;
        }
        throw new BizException(errorCode.getCode(), msg);
    }
}
